﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace NET6Demo.WebApi.Controllers
{
    /// <summary>
    /// AOP-Filter
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class AuthApiController : ControllerBase
    {
        private readonly ILogger<AuthApiController> _logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public AuthApiController(ILogger<AuthApiController> logger)
        {
            _logger = logger;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        //场景一：如果要验证多个同时具备，同时具备多个role,就标记多个 Authorize，分别把角色写上,多个角色是并且的关系
        //[Authorize(AuthenticationSchemes= JwtBearerDefaults.AuthenticationScheme,Roles = "teacher")]
        //[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "admin")]
        //[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "student")] 

        //场景二：多个角色，只要有一个角色匹配即可，多个角色为或者的关系,只需要标记一个Authorize特性，roles="角色名称以逗号分割"，逗号分割的角色名称是或者的关系；
        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "teacher,xxxx")]
        [HttpGet]
        public IActionResult GetUser(int id)
        {
            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(data);
        }

        /// <summary>
        /// 策略授权
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "Policy001")]
        [Route("GetUserPolicy")]
        [HttpGet]
        public IActionResult GetUserPolicy(int id)
        {
            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(data);
        }

        [Route("GetUserPolicyRequirement")]
        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "Policy002")]
        [HttpGet]
        public IActionResult GetUserPolicyRequirement(int id)
        {
            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(data);
        }


    }
}
